package com.navi.loaddata.loadService.impl.hisData.cell1;

import com.navi.loaddata.core.etlException.ETLException;
import com.navi.loaddata.core.mesEntity.MesCtProductlocationhist;
import com.navi.loaddata.core.mesEntity.MesProducthistory;
import com.navi.loaddata.core.oracleEntity.cell1.EtlCellComponentHstV;
import com.navi.loaddata.core.repository.impl.MesRepositoryImpl;
import com.navi.loaddata.core.util.LoggerUtil;
import com.navi.loaddata.core.util.ProcessUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author jay
 */
@Service
public class ProcessHisDataServiceCELL1 {
    private static final Logger logger = LoggerFactory.getLogger(ProcessHisDataServiceCELL1.class);
    @Autowired
    private MesRepositoryImpl mesdao;

    public List<EtlCellComponentHstV> processHis(Map<String, MesProducthistory> hisTrackIn, MesProducthistory hisOut, int errorProcessArrayGlassHstV) throws ETLException {
        List<EtlCellComponentHstV> ldrArrayGlassHstTList = new ArrayList<>();
        try {
            EtlCellComponentHstV etlCellComponentHstV = new EtlCellComponentHstV();

            if ("A".equals(hisOut.getProductname().substring(0,1))) {
                etlCellComponentHstV.setComponentType("1");
            }else if("C".equals(hisOut.getProductname().substring(0,1))) {
                etlCellComponentHstV.setComponentType("4");
            }else{
                etlCellComponentHstV.setComponentType("10");
            }

            etlCellComponentHstV.setStepId(hisOut.getOldprocessoperationname());
            etlCellComponentHstV.setComponentId(hisOut.getProductname());
            etlCellComponentHstV.setGlassId(hisOut.getProductname());
            etlCellComponentHstV.setComponentEndTime(hisOut.getEventtime());
            if(null==hisTrackIn.get(hisOut.getProductname() + "_" + hisOut.getLinkkey())){
                throw new ETLException("errorProcessArrayGlassHstV:Linkkey IS NULL ");
            }
            Date glassStartTime = hisTrackIn.get(hisOut.getProductname() + "_" + hisOut.getLinkkey()).getEventtime();
            etlCellComponentHstV.setComponentStartTime(glassStartTime);
            etlCellComponentHstV.setProductId(hisOut.getProductspecname());
            etlCellComponentHstV.setProductVersion(hisOut.getProductspecversion());
            etlCellComponentHstV.setGroupId(ProcessUtil.processString(hisOut.getLotname()));
            etlCellComponentHstV.setEquipId(hisOut.getMachinename());
            etlCellComponentHstV.setRecipeId(hisOut.getMachinerecipename());

//        MesCtProductlocationhist mesCtProductlocationhist=new MesCtProductlocationhist();
            String hql = "FROM MesCtProductlocationhist WHERE EVENTNAME='ProductOut' AND TRACELEVEL<>'P' and  LINKKEY='" + hisOut.getLinkkey() + "' and PRODUCTNAME='" + hisOut.getProductname() + "' and SUBFACTORYNAME='" + hisOut.getShipid() + "' order by EVENTTIME ASC";
            String chamberPath = "";
            String chamberTime = "";
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            List<MesCtProductlocationhist> mesCtProductlocationhistList = mesdao.list(MesCtProductlocationhist.class, hql.toString());
            if (mesCtProductlocationhistList.size() > 0) {
                for (int i = 0; i < mesCtProductlocationhistList.size(); i++) {
                    chamberPath = chamberPath + mesCtProductlocationhistList.get(i).getUnitname();
                    chamberTime = chamberTime + sdf.format(mesCtProductlocationhistList.get(i).getEventtime().getTime());
                }
            }
            etlCellComponentHstV.setChamberPath(chamberPath);
            etlCellComponentHstV.setChamberTime(chamberTime);
            etlCellComponentHstV.setTaShift(hisOut.getEventname());
            etlCellComponentHstV.setRunMode(hisOut.getRunmode());
            etlCellComponentHstV.setOwnerId(hisOut.getOwnerid());
            etlCellComponentHstV.setOwnerType(hisOut.getOwnertype());
            //8
            etlCellComponentHstV.setCstId(hisOut.getCarriername());
            etlCellComponentHstV.setSlotNo(String.valueOf(hisOut.getPosition()));
            etlCellComponentHstV.setCstIdMoveIn(hisOut.getCarriername());
            etlCellComponentHstV.setSlotNoMoveIn(String.valueOf(hisOut.getPosition()));
            //13-14
            etlCellComponentHstV.setPortIdMoveIn(hisOut.getProcessflowname());
            etlCellComponentHstV.setPortIdMoveOut(hisOut.getEventuser());
            etlCellComponentHstV.setMesLinkKry(hisOut.getLinkkey());
            if (hisOut.getProductspecname().length() >= 7) {
                if ("OC".equals(hisOut.getFactoryname())) {
                    if ("L".equals(hisOut.getProductspecname().substring(0, 1))) {
                        etlCellComponentHstV.setPanelSize(hisOut.getProductspecname().substring(2, 7));
                    } else {
                        etlCellComponentHstV.setPanelSize(hisOut.getProductspecname().substring(1, 6));
                    }
                } else {
                    etlCellComponentHstV.setPanelSize(hisOut.getProductspecname().substring(2, 7));
                }
            }
            //19-21
            etlCellComponentHstV.setHoldFlag(hisOut.getProductholdstate());
            etlCellComponentHstV.setHoldDept(hisOut.getHolddepartment());
            etlCellComponentHstV.setQtapId(hisOut.getQtapgroupname());
            etlCellComponentHstV.setLotPriority(String.valueOf(hisOut.getPriority()));
            etlCellComponentHstV.setGlassGrade(hisOut.getProductgrade());
            etlCellComponentHstV.setGlassJudge(hisOut.getProductjudge());
            etlCellComponentHstV.setPanelGrades(hisOut.getPanelgrades());
            etlCellComponentHstV.setPanelOxInfo(hisOut.getPaneloxs());
            etlCellComponentHstV.setNgMark(hisOut.getNgmark());
            etlCellComponentHstV.setPanelQty(Long.valueOf(getNum(hisOut.getPaneltotalqty())));
            etlCellComponentHstV.setPanelQtyGood(Long.valueOf(getNum(hisOut.getGoodpanelgradeqty())));
            etlCellComponentHstV.setDefectCode(hisOut.getDefectcodes());
            etlCellComponentHstV.setReworkCnt(hisOut.getReworkcount());
            etlCellComponentHstV.setRecycleCnt(hisOut.getRecyclecount());
            //32
            etlCellComponentHstV.setExpNo(hisOut.getExpno());
            etlCellComponentHstV.setTecnId(hisOut.getTecnname());
            etlCellComponentHstV.setMaskId(hisOut.getMaskname());
            etlCellComponentHstV.setMtrlPartNo(hisOut.getTftpolpartno());
            etlCellComponentHstV.setMtrlLotNo(hisOut.getTftpollotname());
            if ("A".equals(hisOut.getProductname().substring(0,1))) {
                etlCellComponentHstV.setArrayGlassId(hisOut.getProductname());
            }else if("C".equals(hisOut.getProductname().substring(0,1))){
                etlCellComponentHstV.setCfGlassId(hisOut.getProductname());
            }else{
                etlCellComponentHstV.setCfGlassId("glass不是A,C开头");
            }

            ldrArrayGlassHstTList.add(etlCellComponentHstV);

        }catch (Exception e){
            logger.error(errorProcessArrayGlassHstV+":"+e.getMessage());
            logger.error(LoggerUtil.stackTraceToString(e));
            throw new ETLException(errorProcessArrayGlassHstV+":"+e.getMessage());
        }
        return ldrArrayGlassHstTList;
    }


    private Long getNum(String str) {
        Long num;
        try {
            num = Long.valueOf(str);
            return num;
        } catch (Exception e) {
//            logger.info("FormatException: BigDecimal[{}] cannot be cast to NUMBER", str);
            num = Long.valueOf(-1);
            return num;
        }
    }
}
